package DataStructure;

import java.io.FileWriter;
import java.io.IOException;

/**
 * 稀疏数组
 *
 * @time: 2021/3/15/015 22:39
 * @author: Zhurunhe
 * <p>
 * 将一个二维数组用稀疏数组存储并保存到磁盘 然后在取出来复原二维数组
 */
public class SparseArray {
    public static void main(String[] args) {

        //创建二维数组 由五行四列组成
        int sparseArray[][] = new int[5][4];
        sparseArray[0][0] = 3;
        sparseArray[0][3] = 7;
        sparseArray[1][2] = -1;
        sparseArray[2][0] = 2;
        //sparseArray[3][2] = 9;
        sparseArray[4][3] = -8;
        int sum = 0;//计算非零的数
        for (int i = 0; i < sparseArray.length; i++) {//行数
            for (int j = 0; j < sparseArray[i].length; j++) {//列数
                if (sparseArray[i][j] != 0) {
                    sum++;
                }
                System.out.print(sparseArray[i][j] + " ");
            }
            System.out.println("\n");
        }
        //System.out.println(sum);
        System.out.println("----------------");


        //将二维数组转为稀疏数组
        int spaArray1[][] = new int[sum + 1][3];
//        spaArray1[0][0] = 5;
//        spaArray1[0][1] = 4;

        for (int i = 0; i < sparseArray.length; i++) {
            spaArray1[0][0] = sparseArray.length;
            spaArray1[0][1] = sparseArray[i].length;
            ;
        }
        spaArray1[0][2] = sum;
        int count = 0;
        for (int i = 0; i < sparseArray.length; i++) {//行数
            for (int j = 0; j < sparseArray[i].length; j++) {//列数
                if (sparseArray[i][j] != 0) {
                    count++;
                    spaArray1[count][0] = i;
                    spaArray1[count][1] = j;
                    spaArray1[count][2] = sparseArray[i][j];
                }
            }
        }
        for (int a = 0; a < spaArray1.length; a++) {
            for (int b = 0; b < spaArray1[a].length; b++) {
                System.out.print(spaArray1[a][b] + " ");
            }
            System.out.println("\n");
        }
        System.out.println("----------------");


        //再将稀疏数组转为二维数组

        int newArray[][] = new int[spaArray1[0][0]][spaArray1[0][1]];
        for(int i=1;i<spaArray1.length;i++){
            newArray[spaArray1[i][0]][spaArray1[i][1]]=spaArray1[i][2];

        }
        for (int a = 0; a < newArray.length; a++) {
            for (int b = 0; b < newArray[a].length; b++) {
                System.out.print(newArray[a][b] + " ");
            }
            System.out.println("\n");
        }


        //将二维数组存放到磁盘中IO流
        FileWriter fileWriter= null;
        try {
            fileWriter = new FileWriter("G:/二维数组.txt");
            for (int a = 0; a < newArray.length; a++) {
                for (int b = 0; b < newArray[a].length; b++) {
                    fileWriter.write(newArray[a][b]+"\t");
                }
                fileWriter.write("\r\n");
            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fileWriter!=null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


}
